<?php

namespace app\admin\controller\qingdongams\customer;

use app\admin\controller\qingdongams\Base;
use addons\qingdongams\model\CustomerProduct as CustomerProductModel;

/**
 * 客户产品
 * @icon fa fa-user
 */
class CustomerProduct extends Base {
	protected $relationSearch = true;
	/**
	 * @var \addons\qingdongams\model\CustomerProduct
	 */
	protected $model = null;


	public function _initialize() {
		parent::_initialize();
		$this->model = new CustomerProductModel();
	}


	/**
	 * 查看
	 */
	public function index() {
		//设置过滤方法
		$this->request->filter(['strip_tags', 'trim']);
		if ($this->request->isAjax()) {
			//如果发送的来源是Selectpage，则转发到Selectpage
			if ($this->request->request('keyField')) {
				return $this->selectpage();
			}
			list($where, $sort, $order, $offset, $limit) = $this->buildparams();
			$list = $this->model->where($where)->with([
				'customer',
                'createStaff',
				'product',
				'contracts'
			])->order($sort, $order)->paginate($limit);



			$result = array("total" => $list->total(), "rows" => $list->items());

			return json($result);
		}

		return $this->view->fetch();
	}


    /**获取附件记录
     */
	public function get_file($ids = null) {
		list($where, $sort, $order, $offset, $limit) = $this->buildparams();
		$list = \addons\qingdongams\model\ContractFile::where(['contract_id' => $ids])->with(['file'])->field('file_id')->paginate($limit);

		$result = array("total" => $list->total(), "rows" => $list->items());

		return json($result);
	}


	public function detail($ids = null) {
		$row = $this->model->where(['id' => $ids])->with([
			'customer',
			'product',
			'contracts',
            'equipment'
		])->find();
        if(empty($row)){
            $this->error('参数不存在');
        }
		if(isset($row['equipment']) && isset($row['equipment']['logo'])){
            $row['equipment']['logo'] = cdnurl($row['equipment']['logo'],true);
        }
		//跟进记录
		$this->assign('row', $row);
		$this->assign('ids', $ids);

		return $this->view->fetch();
	}

    /**获取客户工单
     */
    public function get_workorder($ids = null) {
        $ids=intval($ids);
        list($where, $sort, $order, $offset, $limit) = $this->buildparams();
        $condition ="FIND_IN_SET($ids, customer_product_id)";
        $list = \addons\qingdongams\model\Workorder::where($condition)->paginate($limit);

        $result = array("total" => $list->total(), "rows" => $list->items());

        return json($result);
    }

    /**获取回款计划
     */
	public function get_receivables_plan($ids = null) {
		list($where, $sort, $order, $offset, $limit) = $this->buildparams();
		$list = \addons\qingdongams\model\ReceivablesPlan::where([
			'contract_id' => $ids,
		])->with(['customer'])->paginate($limit);

		$result = array("total" => $list->total(), "rows" => $list->items());

		return json($result);
	}


    /**获取回款记录
     */
	public function get_receivables($ids = null) {
		list($where, $sort, $order, $offset, $limit) = $this->buildparams();
		$list = \addons\qingdongams\model\Receivables::where([
			'contract_id' => $ids,
		])->with(['customer'])->paginate($limit);

		$result = array("total" => $list->total(), "rows" => $list->items());

		return json($result);
	}



}
